package com.yl.zqy.service.impl.sys;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yl.zqy.common.Result;
import com.yl.zqy.dto.SysMenuDTO;
import com.yl.zqy.entity.sys.SysMenu;
import com.yl.zqy.mapper.sys.SysMenuMapper;
import com.yl.zqy.service.sys.SysMenuService;
import com.yl.zqy.utils.ConvertUtils;
import com.yl.zqy.utils.TreeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.yl.zqy.constant.ZQYConstant.SUPER_ADMIN_CODE;

/**
 * <p>
 * 菜单管理 服务实现类
 * </p>
 *
 * @author liang.yu
 * @since 2024-03-25
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Override
    public Result<Boolean> insertMenu(SysMenu menu) {
        int res = baseMapper.insert(menu);
        if (res > 0)
            return Result.ok(true);
        return Result.error(false, "新增失败！");
    }

    @Override
    public Result<Boolean> updateMenu(SysMenu menu) {
        int res = baseMapper.updateById(menu);
        if (res > 0)
            return Result.ok(true);
        return Result.error(false, "修改失败！");
    }

    @Override
    public List<SysMenuDTO> getListPid(String pid) {
        List<SysMenu> menuList = baseMapper.getListPid(pid);
        return ConvertUtils.sourceToTarget(menuList, SysMenuDTO.class);
    }

    @Override
    public List<SysMenuDTO> menuAllList(String status, String name) {
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(status), SysMenu::getStatus, status)
                .and(StringUtils.isNotBlank(name), c -> c.like(SysMenu::getName, name))
                .orderByAsc(SysMenu::getSort);
        List<SysMenu> sysMenus = baseMapper.selectList(queryWrapper);
        List<SysMenuDTO> dtoList = ConvertUtils.sourceToTarget(sysMenus, SysMenuDTO.class);
        return TreeUtils.build(dtoList);
    }

    @Override
    public List<SysMenuDTO> menuSelectTree() {
        List<SysMenu> sysMenuList = baseMapper.selectList(null);
        List<SysMenuDTO> dtoList = ConvertUtils.sourceToTarget(sysMenuList, SysMenuDTO.class);
        SysMenuDTO sysMenuDTO = new SysMenuDTO();
        sysMenuDTO.setPid("top");
        sysMenuDTO.setId("0");
        sysMenuDTO.setName("根目录");
        dtoList.add(0, sysMenuDTO);
        return TreeUtils.build(dtoList);
    }

    @Override
    public List<SysMenuDTO> menus() {
        List<SysMenuDTO> dtoList;
        List<String> roleList = StpUtil.getRoleList();
        if (roleList.contains(SUPER_ADMIN_CODE)) {
            List<SysMenu> sysMenuList = baseMapper.selectList(null);
            dtoList = ConvertUtils.sourceToTarget(sysMenuList, SysMenuDTO.class);
        } else {
            List<SysMenu> userMenus = baseMapper.getUserMenus(StpUtil.getLoginIdAsString());
            dtoList = ConvertUtils.sourceToTarget(userMenus, SysMenuDTO.class);
        }

        return dtoList;
    }

    @Override
    public List<String> getUserByMenusPermission(String userId) {
        return baseMapper.getUserByMenusPermission(userId);
    }
}
